﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Configuration;

public static class StringExtension
{

    #region Public Methods
    /// <summary>
    /// Method to convert the actual url into a contextual unique url
    /// </summary>
    /// <param name="url">The actual url</param>
    /// <param name="PageName">Name of the page</param>
    /// <returns></returns>
    static public string ContextualURL(this string url, string PageName)
    {
        string destID = string.Empty;
        string destAlias = string.Empty;
        string destPage = url.Split('?')[0];
        string parameters = url.Split('?')[1].Replace('=', '/').Replace('&', '/');
        string NewUrl = string.Empty;

        //Get all the Key/Value pairs from web.config
        string[] KeyList = WebConfigurationManager.AppSettings.AllKeys;

        //Iterate the collection to find the specfic key/value pair
        for (int KeyCount = 0; KeyCount < KeyList.Length; KeyCount++)
        {
            destID = WebConfigurationManager.AppSettings[KeyList[KeyCount]].Trim();
            destAlias = KeyList[KeyCount];
            if (destID.ToLower() == destPage.Trim().ToLower())
            {
                // Processed
                break;
            }
        }

        //Form the contextual URL
        NewUrl = destAlias + parameters + "/" + PageName.RemoveMarks().RemoveSpecialCharacters() + ".aspx";

        return NewUrl;
    }

    static public string RemoveBadCode(this string input)
    {
        return input.RemoveBetween("<!--[if gte mso", "<!--[endif] -->");
    }

    static string RemoveBetween(this string input, char begin, char end)
    {
        Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
        return regex.Replace(input, string.Empty);
    }

    static string RemoveBetween(this string input, string start, string end)
    {
        if (null == input) return null;

        string regex = string.Format("{0}(.*?){1}",
            Regex.Escape(start),
            Regex.Escape(end));

        string badCode = Regex.Matches(input, regex, RegexOptions.Singleline)
        .Cast<Match>()
        .Select(match => match.Groups[1].Value).FirstOrDefault();

        if (String.Empty != badCode)
        {
            badCode = start + badCode + end;
            return input.Replace(badCode, String.Empty);
        }

        return input;
    }

    static string RemoveSpecialCharacters(this string input)
    {
        Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
        return r.Replace(input, String.Empty).SpaceToStroke();
    }

    static string RemoveMarks(this string input)
    {
        Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
        string strFormD = input.Normalize(System.Text.NormalizationForm.FormD);
        return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
    }
    #endregion

    #region Private Methods
    static string SpaceToStroke(this string input)
    {
        input = input.Replace(' ', '-');
        while (input.Contains("--"))
        {
            input = input.Replace("--", "-");
        }
        return input;
    }
    #endregion

}